package com.action.admin.function;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.action.BaseAction;
import com.dao.FunctionDao;
import com.entity.Function;
import com.util.Page;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;

/**
 * 
 * @author Administrator
 * 
 */
// @ParentPackage("json-default")
@ParentPackage("extendsjson-default")
@Namespace("/function")
@ResultPath("/WEB-INF/pages")
public class FunctionAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(FunctionAction.class);

	/**
	 * 增加功能
	 * 
	 * @return
	 */
	@Action(value = "addfunction", results = { @Result(name = "success", location = "addfunction.jsp") })
	public String addfunction() {
		try {
			//list = functionDao.findTreeTopNode();
			list = functionDao.findFunctionList();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}


	
//	/**
//	 * 增加功能
//	 * 
//	 * @return
//	 */
//	@Action(value = "findFunctionList", results = { @Result(name = "success", type = "json") })
//	public String findFunctionList() {
//		try {
//			list = functionDao.findFunctionList();
//			this.Object2Json("["+listToMap2(list)+"]");
//			System.out.println("gson.toJson(list)=== "+listToMap2(list));
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return SUCCESS;
//	}
	
	/**
	 * 增加功能
	 * 
	 * @return
	 */
	@Action(value = "checkfunctionname", results = { @Result(name = "success", type = "json") })
	public String checkfunctionname() {
		try {
			flag = functionDao.checkName(functionname);
			if(flag){
				this.outJsonString("0");
			}else{
				this.outJsonString("1");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 修改功能
	 * 
	 * @return
	 */
	@Action(value = "updatefunction", results = { @Result(name = "success", location = "addfunction.jsp") })
	public String updatefunction() {
		try {
			function = functionDao.fundById(id);
			list = functionDao.findTreeTopNode();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存功能
	 * 
	 * @return
	 */
	@Action(value = "savefunction", results = { @Result(name = "success", type = "json") })
	public String savefunction() {
		try {
			if(function != null)
			if (function.getId()!=0){
				flag = functionDao.save(function);
			}else{
				flag = functionDao.updateFunction(function);
			}
			if (flag) {
				this.outJsonString("0");
			} else {
				this.outJsonString("1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 * 保存功能
	 * 
	 * @return
	 */
	@Action(value = "delfunction", results = { @Result(name = "success", type = "json") })
	public String delfunction() {
		try {
			flag = functionDao.del(ids);
			if (flag) {
				this.outJsonString("0");
			} else {
				this.outJsonString("1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

	/**
	 *功能列表
	 * 
	 * @return
	 */
	@Action(value = "functionlist", results = { @Result(name = "success", location = "functionlist.jsp") })
	public String functionlist() {
		try {
			page = functionDao.findbyPage(page);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}
		return SUCCESS;
	}

	private FunctionDao functionDao = (FunctionDao) ioc.newInstance(FunctionDao.class);
	private boolean flag = false;
	private String stateus;
	private List list;
	private Function function;
	private String ids;
	private String functionname;
	private Map map;
	public Map getMap() {
		return map;
	}

	public void setMap(Map map) {
		this.map = map;
	}

	public String getFunctionname() {
		return functionname;
	}

	public void setFunctionname(String functionname) {
		this.functionname = functionname;
	}

	private int id;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	private Page page = new Page();

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public Function getFunction() {
		return function;
	}

	public void setFunction(Function function) {
		this.function = function;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public String getStateus() {
		return stateus;
	}

	public void setStateus(String stateus) {
		this.stateus = stateus;
	}

	public boolean isFlag() {
		return flag;
	}

	public void setFlag(boolean flag) {
		this.flag = flag;
	}
}
